/*
 * Copyright (C) 2019 yixun All Rights Reserved.
 * 
 * WxmsgModelServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.CompanyMapper;
import com.yixun.qfbao.mapper.WxmsgModelMapper;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.WxmsgModel;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.WxmsgModelDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.WxmsgModelService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;


/**
 * [文件名称]<br>
 * WxmsgModelServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 *  service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2019-05-08 15:00:39 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("wxmsgModelService")
public class WxmsgModelServiceImpl implements WxmsgModelService {
	
	@Autowired
	private WxmsgModelMapper mapper;
	@Autowired
	private CompanyMapper companyMapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<WxmsgModel> getPageModel(WxmsgModelDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<WxmsgModel> rows = mapper.list(entity,begin,end);
	    Page<WxmsgModel> msr = new Page<WxmsgModel>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<WxmsgModel> selectListByDto(WxmsgModelDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public WxmsgModel selectByDto(WxmsgModelDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param id 搜索条件
	 * @return 分页记录
	 */
	public WxmsgModel selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public WxmsgModelDto create(WxmsgModelDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(WxmsgModelDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(WxmsgModelDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(WxmsgModelDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public WxmsgModel selectBy(WxmsgModel entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<WxmsgModel> selectListByDtoAll(WxmsgModelDto entity){
		return mapper.selectListByDtoAll(entity);
	}

	/**
	 * 根据模板微信id更新可用状态
	 * @param entity
	 * @return
	 */
	public int updateByMsgId(WxmsgModelDto entity){
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.editByMsgId(entity);
	}

	/**
	 * 获取微信模板消息开关
	 * @param companyId
	 * @param companyUuid
	 * @param domain
	 * @param modelType
	 * @return
	 */
	@Override
	public boolean checkWeixinModelSet(Long companyId,String companyUuid,String domain,int modelType){
		//获取公司信息
		if(companyId == null) {
			CompanyDto companyDto = new CompanyDto();
			if(StringUtils.isEmpty(companyUuid)){
				companyDto.setDomain(domain);
			}else{
				companyDto.setCompanyUuid(companyUuid);
			}

			companyDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			List<Company> companyList = companyMapper.selectListByDto(companyDto);
			if (companyList == null || companyList.size() == 0) {
				return false;
			}
			companyId = companyList.get(0).getId();
		}

		WxmsgModelDto wxmsgModelDto = new WxmsgModelDto();
		wxmsgModelDto.setCompanyId(companyId);
		wxmsgModelDto.setWxModelType(modelType);
//		wxmsgModelDto.setEnabled(1);//可用状态
		wxmsgModelDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		List<WxmsgModel> wxmsgModels =  mapper.selectListByDto(wxmsgModelDto);
		if(wxmsgModels == null || wxmsgModels.size() == 0){
			return false;
		}
		return true;
	}

}
